package com.hmyuan.controller.back;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hmyuan.domain.Tag;
import com.hmyuan.service.TagService;
import com.hmyuan.util.UserUtil;

/**
 * 标签controller
 * @author hmyuan
 *
 */
@Controller
@RequestMapping("/back/tag")
public class TagController {

	@Resource
	TagService tagService;
	/**
	 * 标签管理页面
	 */
	@RequestMapping("/tagPage")
	public String tagPage(HttpServletRequest request,Model model){
		return "/back/tag/tagPage";
	}
	/**
	 * 分页查询标签列表
	 */
	@RequestMapping("/findtagList")
	public String findtagList(HttpServletRequest request,Model model,Integer pageNum,Tag tag){
		if(pageNum==null) pageNum=1;
		request.setAttribute("pageInfo", tagService.selectListByPage(tag, pageNum, 15));
		return "/back/tag/tagPageList";
	}
	/**
	 * 标签新增页面
	 */
	@RequestMapping("/tagAddPage")
	public String tagAddPage(HttpServletRequest request,Model model){
		return "/back/tag/tagAddPage";
	}
	/**
	 * 新增标签页面提交
	 */
	@ResponseBody
	@RequestMapping("/addtag")
	public String addtag(HttpServletRequest request,Model model,Tag tag){
		tag.setUseraccount(UserUtil.getCurrentUser(request).getAccountname()); 
		tag.setCreatetime(new Date());
		tag.setUsenum(0l);
		tag.setStatus(0);
		tagService.insert(tag);
		return "success";
	}
	/**
	 * 编辑标签信息页面
	 */
	@RequestMapping("/tagUpdatePage")
	public String tagUpdatePage(HttpServletRequest request,Model model,Long id){
		if(null==id) return "/tag/tagAddPage";
		Tag tag = tagService.selectByPrimaryKey(id);
		request.setAttribute("tag", tag);
		return "/back/tag/tagUpdatePage";
	}
	/**
	 * 编辑标签页面提交
	 */
	@ResponseBody
	@RequestMapping("/updatetag")
	public String updatetag(HttpServletRequest request,Model model,Tag tag){
		if(null==tag.getId()) return "fail";
		tagService.updateByPrimaryKeySelective(tag);
		return "success";
	}
	/**
	 * 根据id删除标签
	 */
	@ResponseBody
	@RequestMapping("/deletetagById")
	public String deletetagById(HttpServletRequest request,Model model,Long id){
		if(null==id) return "fail";
		tagService.deletetagById(id);
		return "success";
	}
}
